package org.springblade.tms.config;

import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.api.R;
import org.springblade.tms.global.exception.CommonException;
import org.springblade.tms.global.exception.SapException;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

@Order(-2147483648)
@RestControllerAdvice
@Slf4j
public class GlobalErrorCustomHandler {


	@ExceptionHandler(Exception.class) //拦截全局异常
	public ResponseEntity getErrorPath(HttpServletRequest request, HttpServletResponse response, Exception e) throws Exception {
		log.error("{}", e);
		R j = new R();
		j.setSuccess(false);
		HttpStatus httpStatus = HttpStatus.OK;
		if (
			e instanceof MissingServletRequestPartException ||
				e instanceof MissingServletRequestParameterException) {
			httpStatus = HttpStatus.BAD_REQUEST;
			j.setCode(httpStatus.value());
			j.setMsg("您提交的参数，服务器无法处理");
		} else if (e instanceof MethodArgumentNotValidException) {
			log.warn("参数验证失败", e.getMessage());
			httpStatus = HttpStatus.BAD_REQUEST;
			j.setCode(httpStatus.value());
			j.setMsg("参数验证失败" + e.getMessage());
		}
		if (e instanceof ConstraintViolationException) {
			log.warn("参数验证失败", e.getMessage());
			httpStatus = HttpStatus.OK;
			j.setCode(httpStatus.value());
			j.setMsg("参数验证失败" + e.getMessage());
			j.setSuccess(false);
		} else if (e instanceof CommonException) {
			CommonException commonException = (CommonException) e;
			httpStatus = HttpStatus.OK;
			j.setCode(200);
			j.setMsg(commonException.getMessage());
			j.setSuccess(false);
		} else if (e instanceof DuplicateKeyException) {
			DuplicateKeyException duplicateKeyException = (DuplicateKeyException) e;
			httpStatus = HttpStatus.OK;
			j.setCode(200);
			j.setMsg(duplicateKeyException.getMessage());
			j.setSuccess(false);
		} else if (e instanceof DataIntegrityViolationException) {
			DataIntegrityViolationException dataIntegrityViolationException = (DataIntegrityViolationException) e;
			httpStatus = HttpStatus.OK;
			j.setCode(200);
			j.setMsg("数据重复:" + dataIntegrityViolationException.getMessage());
			j.setSuccess(false);
		} else if (e instanceof SapException) {
			SapException sapException = (SapException) e;
			httpStatus = HttpStatus.OK;
			j.setCode(400);
			j.setMsg(sapException.getMessage());
			j.setSuccess(false);
		} else if (e instanceof RuntimeException) {
			RuntimeException commonException = (RuntimeException) e;
			httpStatus = HttpStatus.OK;
			j.setCode(200);
			j.setMsg(commonException.getMessage());
			j.setSuccess(false);
		} else {
			throw e;
		}
		return ResponseEntity.status(httpStatus).body(j);
	}
}
